#!/bin/bash
# ***************************************************************
# *                    simula+@metz.ensam.fr                    *
# *	              GNU/linux version 0.2.4                   *
# *            software under General Public License            *
# ***************************************************************
# * copyright  2003,2004,2005,2006,2007 COLLARD Christophe
# * copyright  2003,2004,2005,2006,2007 Laboratoire de Physique et Mcanique des Matriaux (LPMM - UMR 7554)
# * copyright  2003,2004,2005,2006,2007 Laboratoire de Mathmatiques et ses Applications de Valenciennes (LAMAV - EA 4015)
# ***************************************************************/
#    run simula+ tests belongs to Simula+ </br>
#
#    Simula+ is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version. <br><br>
#
#    Simula+ is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details. <br><br>
#
#    You should have received a copy of the GNU General Public License
#    along with Simula+; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


# configure tests to execute
run_maths="no"
run_mol="yes"
run_femol="no"
run_materials="no"
run_materiol="no"
run_heterogeneities="no"
run_sma="no"
run_mpiol="no"


# color definition
GREEN="\\033[1;32m"
NORMAL="\\033[0;39m"
RED="\\033[1;31m"
PINK="\\033[1;35m"
BLUE="\\033[1;34m"
WHITE="\\033[0;02m"
LIGHTWHITE="\\033[1;08m"
YELLOW="\\033[1;33m"
CYAN="\\033[1;36m"


# search for simula+ directory
if [ "$SimulaPlus" = "" ]; then
    cd ..
    SimulaPlus="$(pwd)"
    if [ -d $SimulaPlus/MOL++ ]; then
	echo -e $GREEN"Simula+ found in "$SimulaPlus$NORMAL
    else
	echo -e $RED"Simula+ not found"
	echo -e "Please define simula+ path in SimulaPlus variable or run program from the simula+/tests directory"$NORMAL
	exit
    fi
else
    if [ -d $SimulaPlus/MOL++ ]; then
	echo -e $GREEN"Simula+ found in "$SimulaPlus$NORMAL
    else
	echo -e $RED"Simula+ not found"
	echo -e "Wrong path defined for simula+"$NORMAL
	exit
    fi
fi
export SimulaPlus
export rat=true
cd $SimulaPlus/tests


# look for a queue manager
queue_manager="$(which qsub)"
if [ "$queue_manager" = "" ]; then
    echo -e $BLUE"warning : no queue manager found" $NORMAL
else
    echo -e $GREEN$queue_manager "... found" $NORMAL
fi    
if [ ! -d log ]; then
    echo -e $GREEN"creating log directory" $NORMAL
    mkdir log
fi


# look for a compiler
compiler=$(which icc |grep icc)
if [ "$compiler" = "" ]; then
   compiler=$(which g++ |grep g++)
   if [ "$compiler" = "" ]; then
      echo -e $RED"fatal error : no compiler found" $NORMAL
      exit
   else echo -e $GREEN"using g++ compiler" $NORMAL
   fi
else echo -e $GREEN"using intel C++ compiler" $NORMAL
fi
echo -e $GREEN$($compiler --version) $NORMAL
export compiler


# execution of maths
if [ -f run\ maths -a "$run_maths" = "yes" ]; then
    if [ -f maths.pbs -a "$queue_manager" != "" ]; then
	echo -en $CYAN"submitting maths : " $NORMAL
	qsub maths.pbs&
    else
	sh run\ maths
    fi
fi


# execution of MOL++
if [ -f run\ MOL++ -a "$run_mol" = "yes" ]; then
    if [ -f mol.pbs -a "$queue_manager" != "" ]; then
	echo -en $CYAN"submitting MOL++ : " $NORMAL
	qsub mol.pbs& 
    else
	sh run\ MOL++
    fi
fi


# execution of FEMOL++
if [ -f run\ FEMOL++ -a "$run_femol" = "yes" ]; then
    if [ -f femol.pbs -a "$queue_manager" != "" ]; then
	echo -en $CYAN"submitting FEMOL++ : " $NORMAL
	qsub femol.pbs& 
    else
	sh run\ FEMOL++
    fi
fi


# execution of materials
if [ -f run\ materials -a "$run_materials" = "yes" ]; then
    if [ -f materials.pbs -a "$queue_manager" != "" ]; then
	echo -en $CYAN"submitting materials : " $NORMAL
	qsub materials.pbs& 
    else
	sh run\ materials
    fi
fi


# execution of MateriOL++
if [ -f run\ MateriOL++ -a "$run_materiol" = "yes" ]; then
    if [ -f materiol.pbs -a "$queue_manager" != "" ]; then
	echo -en $CYAN"submitting MateriOL++ : " $NORMAL
	qsub materiol.pbs& 
    else
	sh run\ MateriOL++
    fi
fi


# execution of heterogeneities
if [ -f run\ heterogeneities -a "$run_heterogeneities" = "yes" ]; then
    if [ -f heterogeneities.pbs -a "$queue_manager" != "" ]; then
	echo -en $CYAN"submitting heterogeneities : " $NORMAL
	qsub heterogeneities.pbs& 
    else
	sh run\ heterogeneities
    fi
fi


# execution of sma
if [ -f run\ sma -a "$run_sma" = "yes" ]; then
    if [ -f materiol.pbs -a "$queue_manager" != "" ]; then
	echo -en $CYAN"submitting sma : " $NORMAL
	qsub sma.pbs& 
    else
	sh run\ sma
    fi
fi


# execution of MPIOL++
if [ -f run\ MPIOL++ -a "$run_mpiol" = "yes" ]; then
    if [ -f materiol.pbs -a "$queue_manager" != "" ]; then
	echo -en $CYAN"submitting MPIOL++ : " $NORMAL
	qsub mpiol.pbs& 
    else
	sh run\ MPIOL++
    fi
fi
